
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "reflecxx.h"

static int iarray[10] = { 1, 2, 3, 4 };
static char carray[5] = { 'a' };
static char carray2[5] = { 'a' };
static struct kv { char *key; char *value; } starray[2] = {
    { "name", "dexter" },
    { "name", "deb" },
};

extern struct reflecxx_full_type iarray_type_info;
extern struct reflecxx_full_type carray_type_info;
extern struct reflecxx_full_type carray2_type_info;
extern struct reflecxx_full_type starray_type_info;

int main(int argc, char *argv[])
{
    int retval = EXIT_SUCCESS;
    reflecxx_primitive_t rp;
    char buffer[1024];

    assert(iarray_type_info.rft_type == RT_ARRAY);
    assert(iarray_type_info.rft_type_info.arr->ra_element_type.rft_type ==
	   RT_INT);
    
    rp = reflecxx_array_get(iarray, iarray_type_info.rft_type_info.arr, 0);
    assert(rp.rp_int == 1);

    reflecxx_print(buffer, sizeof(buffer), &iarray_type_info, iarray);
    assert(strcmp(buffer, "[1, 2, 3, 4, 0, 0, 0, 0, 0, 0]") == 0);

    rp.rp_int = 408;
    reflecxx_array_set(iarray, iarray_type_info.rft_type_info.arr, 0, rp);

    reflecxx_print(buffer, sizeof(buffer), &iarray_type_info, iarray);
    assert(strcmp(buffer, "[408, 2, 3, 4, 0, 0, 0, 0, 0, 0]") == 0);
    
    assert(carray_type_info.rft_type == RT_ARRAY);
    assert(carray_type_info.rft_type_info.arr->ra_element_type.rft_type ==
	   RT_CHAR);
    
    rp = reflecxx_array_get(carray, carray_type_info.rft_type_info.arr, 0);
    assert(rp.rp_char == 'a');
    
    assert(carray2_type_info.rft_type == RT_ARRAY);
    rp = reflecxx_array_get(carray2, carray2_type_info.rft_type_info.arr, 0);
    assert(rp.rp_char == 'a');

    assert(starray_type_info.rft_type == RT_ARRAY);
    assert(starray_type_info.rft_type_info.arr->ra_element_type.rft_type ==
	   RT_STRUCT);
    
    rp = reflecxx_array_get(starray, starray_type_info.rft_type_info.arr, 1);
    assert(rp.rp_pointer == &starray[1]);

    {
	struct kv pair = { "name", "masuka" };

	rp.rp_pointer = &pair;
	reflecxx_array_set(starray,
			   starray_type_info.rft_type_info.arr,
			   0,
			   rp);

	assert(strcmp(starray[0].value, pair.value) == 0);
	
	reflecxx_print(buffer, sizeof(buffer), &starray_type_info, starray);
	assert(strcmp(buffer,
		      "[{\"key\":\"name\", \"value\":\"masuka\"},"
		      " {\"key\":\"name\", \"value\":\"deb\"}]") == 0);
    }
    
    return retval;
}
